package com.ljx.itbbs.server.mall.controller;

import com.ljx.itbbs.common.domain.vo.resp.ApiResult;
import com.ljx.itbbs.common.utils.ServletUtil;
import com.ljx.itbbs.server.mall.domain.vo.req.CouponSaveReq;
import com.ljx.itbbs.server.mall.domain.vo.req.CouponUpdateReq;
import com.ljx.itbbs.server.mall.domain.vo.resp.CouponAdminResp;
import com.ljx.itbbs.server.mall.domain.vo.resp.CouponResp;
import com.ljx.itbbs.server.mall.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "优惠券接口")
@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @ApiOperation("查询用户优惠券列表")
    @GetMapping("/user/list")
    public ApiResult<List<CouponResp>> getUserCouponList(){

        return ApiResult.success(couponService.getUserCouponList(ServletUtil.getUserId()));
    }

    @ApiOperation("查询所有优惠券")
    @GetMapping("/list")
    public ApiResult<List<CouponResp>> getCouponList(){

        return ApiResult.success(couponService.getCouponList());
    }

    @ApiOperation("秒杀优惠券")
    @PostMapping("/seckill/{couponId}")
    public ApiResult<Void> seckill(@Valid @PathVariable Long couponId){
        couponService.seckill(ServletUtil.getUserId(), couponId);

        return ApiResult.success();
    }

    @ApiOperation("新增优惠券")
    @PostMapping("/admin/save")
    public ApiResult<Void> adminSaveCoupon(@Valid @RequestBody CouponSaveReq req){
        couponService.saveCoupon(req);

        return ApiResult.success();
    }

    @ApiOperation("修改优惠券信息")
    @PutMapping("/admin/update")
    public ApiResult<Void> adminUpdateCoupon(@Valid @RequestBody CouponUpdateReq req){
        couponService.updateCoupon(req);

        return ApiResult.success();
    }

    @ApiOperation("管理员查询所有优惠券")
    @GetMapping("/admin/list")
    public ApiResult<List<CouponAdminResp>> getAdminCouponList(){

        return ApiResult.success(couponService.getAdminCouponList());
    }
}
